package com.basic.business.order.service.impl;

import com.basic.business.dingTalk.utils.DingTalkUtils;
import com.basic.business.order.domain.TOrdersLog;
import com.basic.business.order.domain.TOrdersStatus;
import com.basic.business.order.domain.TOrdersTimeLine;
import com.basic.business.order.mapper.TOrdersLogMapper;
import com.basic.business.order.mapper.TOrdersStatusMapper;
import com.basic.business.order.mapper.TOrdersTimeLineMapper;
import com.basic.business.order.service.ITOrdersStatusService;
import com.basic.common.core.service.impl.BaseServiceImpl;
import com.basic.common.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;

@Service
public class TOrdersStatusServiceImpl extends BaseServiceImpl<TOrdersStatusMapper,TOrdersStatus> implements ITOrdersStatusService {

    @Resource
    private TOrdersStatusMapper statusMapper;

    @Resource
    private TOrdersLogMapper logMapper;

    @Resource
    private TOrdersTimeLineMapper timeLineMapper;

    @Override
    public List<String> checkTidIsUnique(List<TOrdersStatus> statusList, String oldPst) {
        return statusMapper.checkTidIsUnique(statusList, oldPst);
    }

    @Override
    public List<TOrdersStatus> getOrderStatusInfo(List<TOrdersStatus> statusList) {
        return statusMapper.getOrderStatusInfo(statusList);
    }

    @Transactional
    @Override
    public void updateOrdersStatus(List<TOrdersStatus> statusList) {
        statusMapper.updateOrdersStatus(statusList);

        List<TOrdersTimeLine> timeLines = new ArrayList<>();
        List<TOrdersLog> logs = new ArrayList<>();
        for (TOrdersStatus status : statusList) {
            TOrdersLog log = new TOrdersLog();
            log.setMainSid(status.getSid());
            log.setContent(status.getContent());
            log.setUserId(SecurityUtils.getUserId());
            logs.add(log);

            TOrdersTimeLine timeLine = TOrdersTimeLine.createTOrdersTimeLine(status);
            timeLine.setOrderStatus(status.getOrderStatus());
            timeLines.add(timeLine);
        }
        logMapper.batchInsert(logs);

        timeLineMapper.batchInsert(timeLines);

        // 异步发送钉钉消息
        CompletableFuture.runAsync(() -> sendDingTalkMessage(statusList));
    }

    public void sendDingTalkMessage(List<TOrdersStatus> statusList) {
        statusList.forEach(status -> {
            if (StringUtils.equals("MANUAL-DR", status.getOrderStatus())) { // 异常退回的
                String content = String.format("\uD83D\uDEA8 [温馨提示]异常退回告警，请相关人员查看\n" +
                        "店铺名称：%s \n" +
                        "系统单号：%s \n" +
                        "平台单号：%s \n" +
                        "内部单号：%s \n" +
                        "接单人：%s \n" +
                        "退回人：%s \n" +
                        "退回原因：%s \n" +
                        "退回时间：%s", status.getShopName(), status.getSid(), status.getTid(), status.getShortId(), UserUtils.getNickNameByUserId(status.getCustomer()), UserUtils.getNickNameByUserId(status.getUserId().toString()), status.getContent(), DateUtils.stampToDate(status.getAccessTime()));
                // 通过钉钉发送消息
            DingTalkUtils.send(content,"DR", UserUtils.getMobileByUserId(status.getCustomer()));
            }
        });
    }

}
